﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;

namespace demo
{
    public static class GZipTools
    {
         private static int bufferSize = 1024;

        public static void Zip()
        {
            string test = GetTestString();
            byte[] original = Encoding.UTF8.GetBytes(test);
            byte[] compressed = null;
            byte[] decompressed = null;

            Console.WriteLine("数据的原始长度是:{0}", original.LongLength);
            // 1.进行压缩
            // 1.1 压缩进入内存流
            using (MemoryStream target = new MemoryStream())
            {
                using (GZipStream gzs = new GZipStream(target, CompressionMode.Compress, true))
                {
                    // 1.2 将数据写入压缩流
                    WriteAllBytes(gzs, original);
                }
                compressed = target.ToArray();
                Console.WriteLine("压缩后的数据长度:{0}", compressed.LongLength);
            }
            // 2.进行解压
            // 2.1 将解压后的数据写入内存流
            using (MemoryStream source = new MemoryStream(compressed))
            {
                using (GZipStream gzs = new GZipStream(source, CompressionMode.Decompress, true))
                {
                    // 2.2 从压缩流中读取所有数据
                    decompressed = ReadAllBytes(gzs);
                }
                Console.WriteLine("解压后的数据长度:{0}", decompressed.LongLength);
                Console.WriteLine("解压前后是否相等:{0}", test.Equals(Encoding.UTF8.GetString(decompressed)));
            }
            Console.ReadKey();
        }


        // 01.取得测试数据
        private static string GetTestString()
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < 1000; i++)
            {
                builder.Append("我是测试数据\r\n");
                builder.Append("我是长江" + (i + 1) + "号\r\n");
            }
            return builder.ToString();
        }

        // 02.从一个流总读取所有字节
        private static byte[] ReadAllBytes(Stream stream)
        {
            byte[] buffer = new byte[bufferSize];
            List<byte> result = new List<byte>();
            int read;
            while ((read = stream.Read(buffer, 0, bufferSize)) > 0)
            {
                if (read < bufferSize)
                {
                    byte[] temp = new byte[read];
                    Array.Copy(buffer, temp, read);
                    result.AddRange(temp);
                }
                else
                {
                    result.AddRange(buffer);
                }
            }
            return result.ToArray();
        }

        // 03.把字节写入一个流中
        private static void WriteAllBytes(Stream stream, byte[] data)
        {
            byte[] buffer = new byte[bufferSize];
            for (long i = 0; i < data.LongLength; i += bufferSize)
            {
                int length = bufferSize;
                if (i + bufferSize > data.LongLength)
                {
                    length = (int)(data.LongLength - i);
                }
                Array.Copy(data, i, buffer, 0, length);
                stream.Write(buffer, 0, length);
            }
        }
    }
}
